/*
 * Copyright (C) 2005,2006 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.catonine.desktop;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.WorkbenchListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ops4j.gaderian.Registry;
import org.ops4j.gaderian.impl.RegistryBuilder;

/**
 * Bootstrap Svansprogram and the Gaderian Registry. Starts the zoom.nu desktop.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
public class Svansprogram implements WorkbenchListener {

	private Log log = LogFactory.getLog(getClass());
	private Workbench workbench;

	public Svansprogram() {
		super();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Svansprogram app = new Svansprogram();
		app.run();
	}

	private void run() {
		log.trace("Starting application");
		// Splashscreen
		JFrame splashFrame = new JFrame("Svansprogram");
		splashFrame.setUndecorated(true);
		URL splashURL = this.getClass().getResource("/images/splash.png");
		ImageIcon splashImageIcon = new ImageIcon(splashURL);
		splashImageIcon = new ImageIcon(splashURL);
		splashFrame.getContentPane().add(new JLabel(splashImageIcon));
		splashFrame.pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		splashFrame.setLocation(screenSize.width / 2 - splashFrame.getWidth() / 2,
				screenSize.height / 2 - splashFrame.getHeight() / 2);
		log.trace("Showing splash screen");
		splashFrame.setVisible(true);

		log.trace("Building Gaderian registry");
		Registry registry = RegistryBuilder.constructDefaultRegistry();
		workbench = (Workbench) registry.getService("nu.zoom.swing.desktop.Workbench", Workbench.class);
		workbench.addWorkBenchListener(this);
		log.trace("Starting workbench");
		workbench.start();

		splashFrame.dispose();
	}

	@Override
	public void close() {
	}

	@Override
	public void start() {
		workbench.setTitle("Svansprogram \u00a9 2006 Johan Maasing");
		workbench.setIcon(Svansprogram.getTailIcon());
	}

	public static List<ImageIcon> getTailIcon() {
		URL iconurl = Svansprogram.class.getResource("/images/tail-icon.png");
		ImageIcon icon = new ImageIcon(iconurl);
		ArrayList<ImageIcon> images = new ArrayList<ImageIcon>();
		images.add(icon);
		return images;
	}

	@Override
	public void visible() {
	}
}
